package com.zzyk.main.configuration.exception;

import com.zzyk.main.model.vo.Message;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 统一异常处理
 * 毕竟500页面不友好
 */
@RestControllerAdvice
public class DefaultExceptionHandler {
    // 如果缺少参数 在这里返回提示缺少参数
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Message notFoundException(MissingServletRequestParameterException e) {
        return Message.failed("缺少参数").setCode(400);
    }

    // 如果参数类型不对 在这里提示
    @ExceptionHandler(TypeMismatchException.class)
    public Message typeMismatchException(TypeMismatchException e) {
        e.printStackTrace();
        return Message.failed("参数类型不匹配").setCode(400);
    }

    // 如果用户不存在
    @ExceptionHandler({UnknownAccountException.class, IncorrectCredentialsException.class})
    public Message unknownAccountException(Exception e) {
        return Message.failed("用户名或密码错误").setCode(401);
    }

    // 如果登录失败 如果没有拦截其子类 都会被这个方法拦截
    @ExceptionHandler(AuthenticationException.class)
    public Message authenticationException(AuthenticationException e) {
        return Message.failed("认证失败: " + e.getMessage()).setCode(401);
    }

    // 如果用户不具有需要的权限
    @ExceptionHandler(AuthorizationException.class)
    public Message authorizationException(AuthorizationException e) {
        e.printStackTrace();
        return Message.failed("权限不足").setCode(403);
    }

    // 如果用户被禁用
    @ExceptionHandler(DisabledAccountException.class)
    public Message disabledAccountException(DisabledAccountException e) {
        return Message.failed(e.getMessage()).setCode(401);
    }

    @ExceptionHandler(UserException.class)
    public Message userException(UserException e) {
        return Message.failed(e.getMessage()).setCode(400);
    }

    @ExceptionHandler(DeviceException.class)
    public Message deviceException(DeviceException e) {
        return Message.failed("删除失败：" + e.getMessage());
    }

    // 如果上传文件大于限制
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public Message requestTooBigException(MaxUploadSizeExceededException e) {
        return Message.failed("文件过大，图片最大为10M");
    }

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Message sqlIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e) {
        e.printStackTrace();
        return Message.failed("违反约束条件");
    }

    @ExceptionHandler(CustomException.class)
    public Message CustomException(CustomException e) {
        e.printStackTrace();
        return Message.failed().setCode(401).setMessage(e.getMessage());
    }

    // 捕捉shiro的异常
    @ExceptionHandler(ShiroException.class)
    public Message handleShiroException(ShiroException e) {
        return Message.failed(e.getMessage());
    }


    @ExceptionHandler(HttpException.class)
    public Message httpException(HttpException e) {
        e.printStackTrace();
        return Message.failed("出错了！");
    }
}
